import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:android_base/pages/online/child/setting.dart';
import 'package:android_base/utils/SocketModbusTcp.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'child/action.dart';
import 'child/history.dart';

class OnlinePage extends StatefulWidget {
  const OnlinePage({super.key});
  @override
  State<OnlinePage> createState() => _OnlinePageState();
}

class _OnlinePageState extends State<OnlinePage> {
  final List<BottomNavigationBarItem> navbar = [
    BottomNavigationBarItem(icon: Icon(CupertinoIcons.home), label: "上线扫码"),
    BottomNavigationBarItem(icon: Icon(CupertinoIcons.search), label: '历史记录'),
    BottomNavigationBarItem(icon: Icon(CupertinoIcons.settings), label: '系统设置'),
  ];
  final List tabBodies = [
    OnlineActionPage(),
    OnlineHistoryPage(),
    SettingPage(),
  ];
  int currentIndex = 0;
  var currentPage;

  @override
  void initState() {
    super.initState();
    SocketModbusTcp.loadPreferences();
    currentPage = tabBodies[currentIndex];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(toolbarHeight: 0),
      bottomNavigationBar: BottomNavigationBar(
        items: navbar,
        currentIndex: currentIndex,
        type: BottomNavigationBarType.fixed,
        onTap: (index) {
          setState(() {
            currentIndex = index;
            currentPage = tabBodies[currentIndex];
          });
        },
      ),
      body: currentPage,
    );
  }
}
